package com.ym.manager.admin.user;

import com.ym.datasource.DataSourceType;
import com.ym.datasource.UseDataSource;
import com.ym.manager.admin.user.model.*;
import com.ym.manager.yinsheng.util.RequestData;
import com.ym.manager.yinsheng.util.ServiceEnum;
import com.ym.manager.yinsheng.util.YinShengClient;
import com.ym.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: dhf
 * @ClassName: AppUserService
 * @Date: 2022/8/22 10:13
 * @Description:
 * @Version: 1.0
 */
@Service
public class AppUserService {
    
    @Autowired
    private AppUserDao appUserDao;

    @UseDataSource(DataSourceType.YS_IM)
    public Pages<UserInfo> getList(int pageNum, int limit,String nickName, String mobile, long id, int status,
                                   List<String> aliBlackIdList,String alipayUid,String alipayName) {
        Map<String,Object> map = new HashMap<>();
        int begin = pageNum;
        map.put("begin",begin);
        map.put("limit",limit);
        map.put("mobile",mobile);
        map.put("id",id);
        map.put("nickName",nickName);
        map.put("status",status);
        map.put("alipayUid",alipayUid);
        map.put("alipayName",alipayName);
        int count  = appUserDao.getListCount(map);
        Pages<UserInfo> pages = new Pages<>(count,pageNum==0?1:pageNum/limit,limit);
        List<UserInfo> list = appUserDao.getList(map);
        list = list.stream().map(x->{
            x.amount = StringHandler.fenToYuan(x.amount);
            x.zfbAmount = StringHandler.fenToYuan(x.zfbAmount);
            if(aliBlackIdList.contains(x.alipayUid)){
                x.alipayStatus=1;
            }else{
                x.alipayStatus=2;
            }
            return x;
        }).collect(Collectors.toList());
        pages.setList(list);
        return pages;
    }
    @UseDataSource(DataSourceType.YS_IM)
    public Map<String,Object> merchantInfo(long uid) {
        return appUserDao.merchantInfo(uid);
    }
    @UseDataSource(DataSourceType.YS_IM)
    public Map<String, Object> userAccountInfo(long uid) {
        return appUserDao.userAccountInfo(uid);
    }
    @UseDataSource(DataSourceType.YS_IM)
    public int userAccountUpdate(long uid, long money,String updateTime) {
        return appUserDao.userAccountUpdate(uid,money,updateTime);
    }
    @UseDataSource(DataSourceType.YS_IM)
    public int userAccountSyncRecordInsert(long uid, long oldMoney, long newMoney, long createTime, long optId, String optName) {
        return appUserDao.userAccountSyncRecordInsert(uid, oldMoney, newMoney, createTime, optId, optName);
    }

    @UseDataSource(DataSourceType.YS_IM)
    public int groupApplyStatus(long uid, int status) {
        return appUserDao.groupApplyStatus(uid, status);
    }


    @UseDataSource(DataSourceType.YS_IM)
    public Pages<UserInfo> groupApplyList(int pageNum, int limit, String mobile, int id) {
        Map<String,Object> map = new HashMap<>();
        map.put("begin",pageNum);
        map.put("limit",limit);
        map.put("mobile",mobile);
        map.put("id",id);
        int count  = appUserDao.groupApplyListCount(map);
        Pages<UserInfo> pages = new Pages<>(count,pageNum==0?1:pageNum/limit,limit);
        List<UserInfo> list = appUserDao.groupApplyList(map);
        list = list.stream().map(x->{
            x.amount = StringHandler.fenToYuan(x.amount);
            return x;
        }).collect(Collectors.toList());
        pages.setList(list);
        return pages;
    }
    @UseDataSource(DataSourceType.YS_IM)
    public UserInfo getUserInfoById(long uid){
        return appUserDao.getUserInfoById(uid);
    }

    @UseDataSource(DataSourceType.YS_IM)
    public UserInfo getUserInfoByPhone(String phone){
        return appUserDao.getUserInfoByPhone(phone);
    }

    @UseDataSource(DataSourceType.YS_IM)
    public int updateStatus(long uid, int status) {
        return appUserDao.updateStatus(uid, status);
    }

    public int userWithdrawBlackDel(long uid) {
        return appUserDao.userWithdrawBlackDel(uid);
    }

    public int userWithdrawBlackInsert(long uid, long createTime) {
        return appUserDao.userWithdrawBlackInsert(uid,createTime);
    }

    public int withdrawRecordStatus(long uid, int status) {
        return appUserDao.withdrawRecordStatus(uid,status);
    }

    @UseDataSource(DataSourceType.YS_IM)
    public Pages<AccountFlowVo> accountFlowList(int start, int length, long uid,String type,int payChannel) {
        Map<String,Object> map = new HashMap<>();
        map.put("begin",start);
        map.put("limit",length);
        map.put("uid",uid);
        if(!StringHandler.isEmpty(type) && !type.equals("0")){
            map.put("type",type);
        }else{
            map.put("type","");
        }
        map.put("payChannel",payChannel);
        int count  = appUserDao.accountFlowListCount(map);
        Pages<AccountFlowVo> pages = new Pages<>(count,start==0?1:start/length,length);
        List<AccountFlowVo> listVo = new ArrayList<>();
        List<AccountFlow> list = appUserDao.accountFlowList(map);
        list.forEach(o->{
            listVo.add(o.toVo());
        });
        pages.setList(listVo);
        return pages;
    }

    @UseDataSource(DataSourceType.YS_IM)
    public Pages<RedVo> redList(int start, int length, long uid) {
        Map<String,Object> map = new HashMap<>();
        map.put("begin",start);
        map.put("limit",length);
        map.put("uid",uid);
        int count  = appUserDao.redListCount(map);
        Pages<RedVo> pages = new Pages<>(count,start==0?1:start/length,length);
        List<RedVo> listVo = new ArrayList<>();
        List<Map<String,Object>> list = appUserDao.redList(map);
        list.forEach(o->{
            RedVo redVo = new RedVo(o);
            listVo.add(redVo);
        });
        pages.setList(listVo);
        return pages;
    }

    @UseDataSource(DataSourceType.YS_IM)
    public Pages<TransferVo> transferList(int start, int length, long uid) {
        Map<String,Object> map = new HashMap<>();
        map.put("begin",start);
        map.put("limit",length);
        map.put("uid",uid);
        int count  = appUserDao.transferListCount(map);
        Pages<TransferVo> pages = new Pages<>(count,start==0?1:start/length,length);
        List<TransferVo> listVo = new ArrayList<>();
        List<Map<String,Object>> list = appUserDao.transferList(map);
        list.forEach(o->{
            TransferVo redVo = new TransferVo(o);
            listVo.add(redVo);
        });
        pages.setList(listVo);
        return pages;
    }

    @UseDataSource(DataSourceType.YS_IM)
    public Pages<Map<String, Object>> checkUserAmountList(int start, int length,int beginTime,int endTime,int type) {
        int count  = 0;
        List<Map<String,Object>> list = new ArrayList();
        if(type ==1){
            count = appUserDao.checkUserAmountPayListCount(beginTime,endTime);
            list = appUserDao.checkUserAmountPayList(beginTime,endTime,start,length);
        }
        if(type ==2){
            count = appUserDao.checkUserAmountListCount();
            list = appUserDao.checkUserAmountList(start,length);
        }
        Pages<Map<String, Object>> pages = new Pages<>(count,start==0?1:start/length,length);
        List<Map<String, Object>> listVo = new ArrayList<>();
        list.forEach(o->{
            Map<String,Object> requestMap = new HashMap<>();
            String merchantNo = StringHandler.getString(o.get("merchantNo"));
            if(StringHandler.isNotEmpty(merchantNo)) {
                requestMap.put("merchantNo", merchantNo);
                RequestData requestData = new RequestData(requestMap);
                YinShengClient yinShengClient = new YinShengClient(ServiceEnum.QUERY_AMOUNT);
                String callback = yinShengClient.requestStrApi(requestData);
                Map<String, Object> obj = JsonUtil.jsonToBean(callback, Map.class);
                String cashAmount = "";
                if (!obj.get("code").equals("SYS000")) {
                } else {
                    if (StringHandler.isNotEmpty(StringHandler.getString(obj.get("bizResponseJson")))) {
                        Map<String, Object> bizResponseJson = JsonUtil.jsonToBean(JsonUtil.toJson(obj.get("bizResponseJson")), Map.class);
                        if (bizResponseJson.get("subCode").equals("COM000")) {
                            List<Map<String, Object>> accountList = JsonUtil.jsonToBean(JsonUtil.toJson(bizResponseJson.get("accountList")), List.class);
                            if (StringHandler.isNotEmpty(accountList)) {
                                for (Map<String, Object> accountMap : accountList) {
                                    if ("01".equals(StringHandler.getString(accountMap.get("accountType")))) {
                                        cashAmount = StringHandler.getString(accountMap.get("cashAmount"));
                                    }
                                }
                            }
                        }
                    }
                }
                if((StringHandler.isEmpty(cashAmount)?0:StringHandler.yuanToFen(cashAmount))!=StringHandler.getLong(o.get("balance"))){
                    o.put("balance",StringHandler.fenToYuan(o.get("balance")+""));
                    o.put("cashAmount",cashAmount);
                    o.put("merchantNo",merchantNo);
                    listVo.add(o);
                }
            }
        });
        pages.setList(listVo);
        return pages;
    }

    @UseDataSource(DataSourceType.YS_IM)
    public void addUser(String phone,String password) {
        UserInfo userInfo = new UserInfo();
        userInfo.salt = UUID.randomUUID().toString().substring(0, 4);
        userInfo.password = MD5.md5(password + userInfo.salt);
        userInfo.phone = phone;
        userInfo.nickName = phone.substring(6,phone.length());
        appUserDao.addInfo(userInfo);
        //初始化用户隐私设置
        appUserDao.initUserPrivacyConfig(userInfo);
    }
    @UseDataSource(DataSourceType.YS_IM)
    public int aliBlack(String configValue) {
        return appUserDao.aliBlack(configValue);
    }
    @UseDataSource(DataSourceType.YS_IM)
    public String aliBlackId() {
        return appUserDao.aliBlackId();
    }

    @UseDataSource(DataSourceType.YS_IM)
    public String configValue(String configKey){
        return appUserDao.configValue(configKey);
    }
    @UseDataSource(DataSourceType.YS_IM)
    public int addBlackIp(String ip){
        return appUserDao.addBlackIp(ip);
    }
    @UseDataSource(DataSourceType.YS_IM)
    public int updateBlackIp(String ip) {
        return appUserDao.updateBlackIp(ip);
    }

    @UseDataSource(DataSourceType.YS_IM)
    public int transferUpdate(long transferId, long uid) {
        return appUserDao.transferUpdate(transferId,uid);
    }
    @UseDataSource(DataSourceType.YS_IM)
    public int transferCount(long transferId, long uid) {
        return appUserDao.transferCount(transferId,uid);
    }
    @UseDataSource(DataSourceType.YS_IM)
    public int redUpdate(long redId, long uid) {
        return appUserDao.redUpdate(redId,uid);
    }
    @UseDataSource(DataSourceType.YS_IM)
    public int redCount(long redId, long uid) {
        return appUserDao.redCount(redId,uid);
    }

    @UseDataSource(DataSourceType.YS_IM)
    public int delUserData(long userId) {
        return appUserDao.delUserData(userId);
    }
    @UseDataSource(DataSourceType.YS_IM)
    public UserConfig userConfig(long userId) {
        return appUserDao.userConfig(userId);
    }

    @UseDataSource(DataSourceType.YS_IM)
    public Pages<Map<String,Object>> bankList(int pageNum, int limit,long userId, String card_no) {
        int begin = pageNum;
        int count  = appUserDao.bankListCount(userId,card_no);
        List<Map<String,Object>> list = appUserDao.bankList(begin,limit,userId,card_no);
        Pages<Map<String,Object>> pages = new Pages<>(count,pageNum==0?1:pageNum/limit,limit);
        pages.setList(list);
        return pages;
    }

    @UseDataSource(DataSourceType.YS_IM)
    public int groupBlackUserCount(long groupId, long groupLeaderId, long blackUserId) {
        return appUserDao.groupBlackUserCount(groupId,groupLeaderId,blackUserId);
    }
    @UseDataSource(DataSourceType.YS_IM)
    public int groupBlackUser(long groupId, long groupLeaderId, long blackUserId) {
        return appUserDao.groupBlackUser(groupId,groupLeaderId,blackUserId);
    }
    @UseDataSource(DataSourceType.YS_IM)
    public int groupBlackUserUn(long groupId, long groupLeaderId, long blackUserId) {
        return appUserDao.groupBlackUserUn(groupId,groupLeaderId,blackUserId);
    }
    @UseDataSource(DataSourceType.YS_IM)
    public Map<String, Object> getRedMapInfo(long redId) {
        return appUserDao.getRedMapInfo(redId);
    }
    @UseDataSource(DataSourceType.YS_IM)
    public int getRedDealCount(long redId) {
        return appUserDao.getRedDealCount(redId);
    }
    @UseDataSource(DataSourceType.YS_IM)
    public Pages<UserFriendsInfo> friendList(int start, int length, long uid,int status) {
        Map<String,Object> map = new HashMap<>();
        map.put("begin",start);
        map.put("limit",length);
        map.put("status",status);
        map.put("uid",uid);
        int count  = appUserDao.friendListCount(map);
        Pages<UserFriendsInfo> pages = new Pages<>(count,start==0?1:start/length,length);
        List<UserFriendsInfo> list = appUserDao.friendListList(map);
        pages.setList(list);
        return pages;
    }

    @UseDataSource(DataSourceType.YS_IM)
    public int updateAliByUser(long uid) {
        return appUserDao.updateAliByUser(uid);
    }

    @UseDataSource(DataSourceType.YS_IM)
    public int delData(long userId) {
        appUserDao.delWithdraw(userId);
        return appUserDao.delAccount(userId);
    }

    @UseDataSource(DataSourceType.YS_IM)
    public int bankDel(long id, long uid) {
        return appUserDao.bankDel(id,uid);
    }

    @UseDataSource(DataSourceType.YS_IM)
    public List<Map<String, Object>> redRecordList(long redId) {
        return appUserDao.redRecordList(redId);
    }

    @UseDataSource(DataSourceType.YS_IM)
    public List<Map<String, Object>> getRedList(String endTime) {
        return appUserDao.getRedList(endTime);
    }

    @UseDataSource(DataSourceType.YS_IM)
    public List<Map<String, Object>> getTransList(String endTime) {
        return appUserDao.getTransList(endTime);
    }

    @UseDataSource(DataSourceType.YS_IM)
    public List<BaseUserFriendsPO> getFriendsListByUserId(String userId) {
        return appUserDao.getFriendsListByUserId(userId);
    }
    @UseDataSource(DataSourceType.YS_IM)
    public int friendsCopy(long userId, int type) {
        return appUserDao.friendsCopy(userId,type);
    }
}
